package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.RecyclerViewUtil;
import com.dlc.commonlibrary.widget.CommonItemView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.CountingProductAdapter;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.ProductBean;
import com.dlc.beijingrfid.net.protocol.CountingProto;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CountingProduct2Activity extends BaseActivity {

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;
    @BindView(R.id.btn_count) Button mBtnCount;
    @BindView(R.id.item_where) CommonItemView mItemWhere;
    CountingProductAdapter<ProductBean.DataBean> adapter;
    private String warehouseId;
    private String wareFrameId;
    private String stockno;
    private int counts = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.leftExit(this);

        Intent intent = getIntent();
        String wareHouse = intent.getStringExtra("wareHouse");
        String wareFrame = intent.getStringExtra("wareFrame");
        warehouseId = intent.getStringExtra("warehouseId");
        wareFrameId = intent.getStringExtra("wareFrameId");
        stockno = intent.getStringExtra("stockno");

       // mItemWhere.setLabel(wareHouse + "-" + wareFrame);
        mItemWhere.setLabel(wareHouse );

        mTitleBar.setTitle("货物盘点");
        initData(warehouseId,wareFrameId);

        adapter = new CountingProductAdapter<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_counting_product2;
    }

    @OnClick({ R.id.btn_count })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_count:
                    startActivity(new Intent(this, CountingDetailActivity.class).putExtra("warehouseId", warehouseId)
                        .putExtra("wareFrameId", wareFrameId).putExtra("stockno", stockno).putExtra("counts",counts));
                break;
        }
    }

    /**
     * 获取货架数据
     */
    private void initData(String warehouseId,String wareFrameId) {
        CountingProto.get().getWareFrameSpec(warehouseId,wareFrameId).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<ProductBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(ProductBean productBean) {
                        if(productBean != null && productBean.getData() != null){
                            adapter.setNewData(productBean.getData());
                            for (int i = 0;i < productBean.getData().size();i++ ){
                                counts = counts + Integer.parseInt(productBean.getData().get(i).getCount());
                            }
                        }
                    }
                });
    }
}
